<template>
	<view class="updateMobile">
		<u-navbar title="修改手机号" :autoBack="true" :placeholder="true">
		</u-navbar>


		<view class="inputContent">
			<input type="number" v-model="phone" placeholder="请输入新的手机号" />
		</view>

		<view class="inputContent">
			<input type="number" v-model="code" placeholder="请输入验证码" />
			<view class="line"></view>
			<view class="sencode" :class="{color_student:role==1,color_parent:role==2, disabled: isCounting}"
				@click="handleSendcode">
				{{ innerText }}
			</view>
		</view>
		<view class="btns" :class="{btns_student:role ==1,btns_parent:role==2}" @click="handleSave">
			确认
		</view>

	</view>
</template>

<script>
	import {
		changePhone
	} from "../../../api/mine.js"
	import {
		sendMsg
	} from "../../../api/api.js"
	export default {
		data() {
			return {
				role: uni.getStorageSync("role"),
				phone: "",
				code: "",
				innerText: "发送验证码",
				isCounting: false,
				timer: null,
			}
		},
		onUnload() {
			if (this.timer) {
				clearInterval(this.timer)
				this.timer = null
			}
		},
		methods: {
			async handleSendcode() {
				if (this.isCounting) return
				if (!this.phone) {
					uni.showToast({
						title: '请输入新的手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				try {
					const res = await sendMsg({
						phone: this.phone
					})
					if (res.code === 200) {
						uni.showToast({
							title: '发送成功',
							icon: 'none'
						})
						this.isCounting = true
						let countdown = 60
						this.innerText = `${countdown}s`
						this.timer = setInterval(() => {
							countdown--
							if (countdown <= 0) {
								clearInterval(this.timer)
								this.timer = null
								this.isCounting = false
								this.innerText = '发送验证码'
							} else {
								this.innerText = `${countdown}s`
							}
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg || '验证码发送失败',
							icon: 'none'
						})
					}
				} catch (e) {
					uni.showToast({
						title: '验证码发送失败',
						icon: 'none'
					})
				}
			},

			async handleSave() {
				if (!this.phone) {
					uni.showToast({
						title: '请输入新的手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				if (this.code == '') {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				let res = await changePhone({
					phone: this.phone,
					code: this.code
				})
				if (res.code == 200) {
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.updateMobile {
		width: 100%;

		.inputContent {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 686rpx;
			height: 96rpx;
			border-radius: 16rpx;
			border: 2rpx solid #DDDDDD;
			margin: 30rpx auto;
			padding: 30rpx;
			box-sizing: border-box;

			input {
				width: 65%;
			}

			.line {
				width: 2rpx;
				height: 32rpx;
				background: #DDDDDD;
				border-radius: 4rpx;
			}

			.sencode {
				font-weight: 400;
				font-size: 32rpx;

				&.disabled {
					color: #C9C9C9 !important;
					pointer-events: none;
				}
			}
		}

		.btns {
			width: 636rpx;
		}
	}
</style>